<?php

namespace App\Service\Third;

use App\Helpers\AliyunUtil;
use App\Helpers\ImUtil;
// use App\Helpers\ShumeiUtil;
// use App\Model\BlackWords;
// use App\Model\Message;
use App\Helpers\RedisUtil;
use App\Model\RongcloudResult;
use App\Model\Service\ConsumeService;
use App\Model\Setting\TmpSetting;
// use App\Model\User;
use App\Model\User;
use App\Model\UserProfile;
use App\Service\BaseService;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use App\Model\Message;
/**
 * 融云消息回调
 */
class RongDetectionEffectService extends BaseService
{
    function __construct(DriverFactory $driverFactory)
    {
        parent::__construct($driverFactory);
        $this->queueDriver = $driverFactory->get('default');
    }

    public function response()
    {
        $callbackInfo = $this->request->all();
        if($callbackInfo['result'] == 10001){
            $sendInfo=  json_decode($callbackInfo['content'],true);
            $fromUserId = $sendInfo['fromUserId'];#接收者ID
            $targetId = $sendInfo['targetId']; #目标ID
            $msgType  = ImUtil::MSG_BREAK_THE_RULE;
            $extInfo = [
                'sys_tip' => '平台提倡健康交友，请勿发送涉黄，涉政低俗内容，详见<a href="http://www.xinmiwangluo.com/ffjygf.html"><font color="#EF426C">《平台公约》</font></a>',
            ];
            $imcount = User::getUserIdByIm([$targetId,$fromUserId],'kv');
            if(empty($imcount[$targetId]) && $imcount[$fromUserId]) return false;
            //发送渗透
            go(function () use($fromUserId,$targetId,$msgType,$extInfo) {//
                usleep(300000);
                $res = Message::cannotImIdSend($fromUserId,$targetId,$msgType,$extInfo);
            });
            $message = json_decode($sendInfo['message'],true);
            $content = !empty($message['content']) ? $message['content'] : '';
//            if($sendInfo['objectName'] == ImUtil::RONG_TEXT_TYPE){
//                RongcloudResult::addRecord((int)$imcount[$fromUserId],(int)$imcount[$targetId],$sendInfo['objectName'],$content,$callbackInfo['resultDetail'],$callbackInfo['msgUID']);
//            }
            RongcloudResult::addRecord((int)$imcount[$fromUserId],(int)$imcount[$targetId],$sendInfo['objectName'],$content,$callbackInfo['resultDetail'],$callbackInfo['msgUID'],json_encode($sendInfo));

            return true;
        }else{
            $sendInfo=  json_decode($callbackInfo['content'],true);
            $fromUserIm = $sendInfo['fromUserId'];#接收者ID
            $targetIm = $sendInfo['targetId']; #目标ID
//            ConsumeService::setConsume($fromUserIm,$targetIm);
        }
        return true;
    }


    function getMillisecond() {
        list($s1, $s2) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
    }
}
